package com.thz.friend.rabbit.producer;

import com.thz.common.core.constants.RabbitConstants;
import com.thz.common.core.enums.ResultCode;
import com.thz.common.core.result.Result;
import com.thz.exception.ApplicationException;
import com.thz.friend.pojo.user.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author thz
 * @Date 2025/3/8
 */
@Slf4j
@Component
public class MailProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public boolean sendMobileCode(String email, String code) throws ApplicationException {
        // 校验符合，生成验证码
        UserDTO userEmailSendDTO = new UserDTO();
        userEmailSendDTO.setEmail(email);
        userEmailSendDTO.setCode(code);
        try {
            rabbitTemplate.convertAndSend(RabbitConstants.OJ_MAIL_EXCHANGE, RabbitConstants.OJ_MAIL_KEY, userEmailSendDTO);
            return true;
        } catch (Exception e) {
            log.warn(ResultCode.EMAIL_SEND_ERROR.toString());
            throw new ApplicationException(Result.failed(ResultCode.EMAIL_SEND_ERROR));
        }
    }

}
